package edu.cmu.cs.stage3.alice.authoringtool.editors.compositeeditor;

import edu.cmu.cs.stage3.resourceBundle.I18n;

import edu.cmu.cs.stage3.alice.authoringtool.AuthoringToolResources;

public abstract class WhileElementPanel extends CompositeElementPanel {

 protected javax.swing.JComponent conditionalInput;
 protected javax.swing.JLabel endHeader;
 protected edu.cmu.cs.stage3.alice.core.property.BooleanProperty m_condition;

 public WhileElementPanel(){
  super();
  headerText = I18n.getString("commandT_While"); // "While"
  backgroundColor = AuthoringToolResources.getColor("WhileLoopInOrder");
 }

 //Rem @Override
 protected void variableInit(){
  super.variableInit();
  if (m_element instanceof edu.cmu.cs.stage3.alice.core.response.WhileLoopInOrder){
   edu.cmu.cs.stage3.alice.core.response.WhileLoopInOrder proxy = (edu.cmu.cs.stage3.alice.core.response.WhileLoopInOrder)m_element;
   m_condition = proxy.condition;
  }
  else if (m_element instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.While){
   edu.cmu.cs.stage3.alice.core.question.userdefined.While proxy = (edu.cmu.cs.stage3.alice.core.question.userdefined.While)m_element;
   m_condition = proxy.condition;
  }
 }

 //Rem @Override
 protected void startListening() {
  super.startListening();
  if (m_condition != null){
   m_condition.addPropertyListener(this);
  }
 }

 //Rem @Override
 protected void stopListening() {
  super.stopListening();
  if (m_condition != null){
   m_condition.removePropertyListener(this);
  }
 }

 //Rem @Override
 public void setHeaderLabel(){
  if (headerLabel != null){
   headerLabel.setText(headerText);
   if (CompositeElementEditor.IS_JAVA){
    headerLabel.setText(I18n.getString("commandT_While")); // "while ("
   }
  }
  if (endHeader != null){
   endHeader.setText("");
   if (CompositeElementEditor.IS_JAVA){
    if (!isExpanded){
     endHeader.setText(") { "+getDots()+" }");
    }
    else{
     endHeader.setText(") {");
    }
   }
  }

 }

 //Rem @Override
 protected void generateGUI(){
  super.generateGUI();
  if (endHeader == null){
   endHeader = new javax.swing.JLabel();
  }
 }

 //Rem @Override
 protected void restoreDrag(){
  super.restoreDrag();
  this.addDragSourceComponent(endHeader);
 }

 //Rem @Override
 protected void updateGUI(){
  super.updateGUI();
  edu.cmu.cs.stage3.alice.authoringtool.util.PopupItemFactory pif = new edu.cmu.cs.stage3.alice.authoringtool.util.SetPropertyImmediatelyFactory(m_condition);
  conditionalInput = edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.getPropertyViewController(m_condition, true, true, AuthoringToolResources.shouldGUIOmitPropertyName(m_condition), pif);
  headerPanel.remove(glue);
  headerPanel.add(conditionalInput, new java.awt.GridBagConstraints(3,0,1,1,0,0,java.awt.GridBagConstraints.CENTER, java.awt.GridBagConstraints.NONE, new java.awt.Insets(0,2,0,2), 0,0));
  headerPanel.add(endHeader, new java.awt.GridBagConstraints(4,0,1,1,0,0,java.awt.GridBagConstraints.CENTER, java.awt.GridBagConstraints.NONE, new java.awt.Insets(0,2,0,2), 0,0));
  headerPanel.add(glue, new java.awt.GridBagConstraints(5,0,1,1,1,0,java.awt.GridBagConstraints.CENTER, java.awt.GridBagConstraints.BOTH, new java.awt.Insets(0,0,0,0), 0,0));
 }

 protected static java.awt.image.BufferedImage whileLoopBackgroundImage;
 protected static java.awt.Dimension whileLoopBackgroundImageSize = new java.awt.Dimension( -1, -1 );

 protected void createBackgroundImage( int width, int height ) {
  whileLoopBackgroundImageSize.setSize( width, height );
  whileLoopBackgroundImage = new java.awt.image.BufferedImage( width, height, java.awt.image.BufferedImage.TYPE_INT_ARGB );
  java.awt.Graphics2D g = (java.awt.Graphics2D)whileLoopBackgroundImage.getGraphics();
  g.addRenderingHints( new java.awt.RenderingHints( java.awt.RenderingHints.KEY_ANTIALIASING, java.awt.RenderingHints.VALUE_ANTIALIAS_ON ) );
  g.setColor( backgroundColor );
  g.fillRect( 0, 0, width, height );
 }

}
